package edu.arhs.team1100.worldclass.forms;

import edu.arhs.team1100.worldclass.handlers.TeamHandler;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Eddie
 */
public class ClientApplication extends javax.swing.JFrame {

    /**
     * Creates new form ClientForm
     */
    public ClientApplication() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        menuBar = new javax.swing.JMenuBar();
        teamMenuItem = new javax.swing.JMenu();
        addTeamMenuItem = new javax.swing.JMenuItem();
        teamViewMenuItem = new javax.swing.JMenuItem();
        scoringMenu = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Scouting Client 2015");

        menuBar.setBorder(null);

        teamMenuItem.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        teamMenuItem.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        teamMenuItem.setLabel("Team");
        teamMenuItem.setMargin(new java.awt.Insets(0, 23, 0, 0));
        teamMenuItem.setPreferredSize(new java.awt.Dimension(85, 19));

        addTeamMenuItem.setText("Add");
        addTeamMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTeamMenuItemActionPerformed(evt);
            }
        });
        teamMenuItem.add(addTeamMenuItem);

        teamViewMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        teamViewMenuItem.setText("View All");
        teamViewMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teamViewMenuItemActionPerformed(evt);
            }
        });
        teamMenuItem.add(teamViewMenuItem);

        menuBar.add(teamMenuItem);

        scoringMenu.setText("Scoring");

        jMenuItem2.setText("Score Matches");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        scoringMenu.add(jMenuItem2);

        menuBar.add(scoringMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 599, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addTeamMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTeamMenuItemActionPerformed
        TeamManualPanel manualTeamPanel = new TeamManualPanel();
        boolean newTeamAdded = false;
        do {
            try {
                int addTeamResult = JOptionPane.showConfirmDialog(new JFrame(), manualTeamPanel, "New Team", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

                if (addTeamResult == JOptionPane.OK_OPTION) {
                    if (TeamHandler.addTeam(manualTeamPanel.getTeam())) {
                        newTeamAdded = true;
                    } else {
                        JOptionPane.showConfirmDialog(new JFrame(), "Team " + manualTeamPanel.getTeam().getTeamNumber() + " already exists in database.", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    break;
                }

            } catch (NumberFormatException e) {
                JOptionPane.showConfirmDialog(new JFrame(), "Please enter a number", "Error", JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);
            }
        } while (!newTeamAdded);

    }//GEN-LAST:event_addTeamMenuItemActionPerformed

    private void teamViewMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teamViewMenuItemActionPerformed
        newJFrame(new TeamViewPanel(TeamHandler.getTeams()), "Teams");
    }//GEN-LAST:event_teamViewMenuItemActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        new ScoringFrame().setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem addTeamMenuItem;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu scoringMenu;
    private javax.swing.JMenu teamMenuItem;
    private javax.swing.JMenuItem teamViewMenuItem;
    // End of variables declaration//GEN-END:variables

    public void display() {
        setVisible(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize((int) screenSize.getWidth(), 60);
    }
    
    private void newJFrame(JPanel panel, String title){
        JFrame jframe = new JFrame();
        jframe.add(panel);
        jframe.pack();
        jframe.setTitle(title);
        jframe.setVisible(true);
    }
}
